#include "list.h"

void List_Add(LIST *list, LIST_NODE  *node)
{
    node->next = 0;
    node->prev = list->tail;

    if (list->tail)
        list->tail->next = node;
    list->tail = node;

    if (list->head == 0)
        list->head = node;
}

void List_Insert(LIST *list, LIST_NODE *before, LIST_NODE *node)
{
    if (before->prev)
        before->prev->next = node;
    node->prev = before->prev;
    node->next = before;
    before->prev = node;

    if (list->head == before)
        list->head = node;
}

void List_Remove(LIST *list, LIST_NODE *node)
{
    if (node->prev)
        node->prev->next = node->next;
    if (node->next)
        node->next->prev = node->prev;

    if (list->head == node)
        list->head = node->next;
    if (list->tail == node)
        list->tail = node->prev;
}
